import { Ref, reactive, ref } from "vue";
// mock数据
import axios from "axios";
// leanCloud请求
import { getDeviceApi } from "@/api/device/device";

interface IBaseData {
  name: string;
  id: number;
  treeData: any;
}
interface propsType {
  label: string;
  children: string;
  isLeaf: string;
}
interface Tree {
  label: string;
  children?: Tree[];
}

export default class deviceTree {
  public baseData: IBaseData;
  public filterValue: Ref<any>;
  public defaultProps: propsType;
  public loading: Ref<boolean>;
  constructor() {
    this.loading = ref(true);
    this.baseData = reactive({
      name: "张三",
      id: 0,
      treeData: [],
    });
    this.filterValue = ref();
    this.defaultProps = reactive({
      label: "nodeName",
      children: "childNodeList",
      isLeaf: "isLeaf",
    });

    this.getTreeData();
  }

  // 获取列表
  getTreeData() {
    this.loading.value = true;
    axios.get("/home/getDeviceAllData").then((res) => {
      const resData = [Object.assign(res.data.data, {})];
      this.baseData.treeData = resData;
      console.log("treeData", this.baseData.treeData);
      this.loading.value = false;
    });

    getDeviceApi().then((res) => {
      console.log("res", res);
    });
  }

  // tree-select 懒加载
  load = (node: any, resolve: any) => {
    console.log("node", node.level);
    if (node.level === 0) {
      axios.get("/home/getDeviceOne").then((res) => {
        const resData = Object.assign(res.data.data.result.devTreeList, {});
        resData[0]["value"] = 0;
        // this.baseData.treeData = resData;
        // console.log("treeData", this.baseData.treeData);
        return resolve(resData);
      });
    }

    if (node.level === 1) {
      axios.get("/home/getDeviceTwo").then((res) => {
        console.log("device2 res", res);
        return resolve(res.data.data.result.devTreeList);
      });
    }

    if (node.level === 2) {
      axios.get("/home/getDeviceThree").then((res) => {
        console.log("device3 res", res);
        return resolve(res.data.data.result.devTreeList[2].childNodeList);
      });
    }
    if (node.level > 2) {
      return resolve([]);
    }
  };

  handleNodeClick = (data: Tree) => {
    console.log(data);
  };
}
